package com.yuecai.modules.yc_user;

import com.yuecai.modules.yc_user.service.YcUserService;
import com.yuecai.modules.yc_user.service.dto.YcUserDto;
import com.yuecai.modules.yc_user.service.dto.YcUserEditAuth;
import com.yuecai.modules.yc_user.service.dto.YcUserQueryCriteria;
import com.yuecai.modules.yc_user.service.mapstruct.YcUserMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private YcUserService ycUserService;

    @Autowired
    private YcUserMapper ycUserMapper;


    @GetMapping
    @ApiOperation("用户")
    public ResponseEntity<Object> query(YcUserQueryCriteria criteria, Pageable pageable){

        return new ResponseEntity<>(ycUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/driver")
    @ApiOperation("用户")
    public ResponseEntity<Object> queryDriver(YcUserQueryCriteria criteria, Pageable pageable){
        if (criteria.getType()==null){
            criteria.setType(2);
        }
        return new ResponseEntity<>(ycUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/getAuth")
    @ApiOperation("用户")
    public ResponseEntity<Object> getAuth(YcUserQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ycUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PutMapping("/editAuth")
    public ResponseEntity<Object> editAuth(@RequestBody YcUserEditAuth ycUserEditAuth) {
        Long userId = ycUserEditAuth.getUserId();
        YcUserDto byId = ycUserService.findById(userId);
        Integer level = ycUserEditAuth.getLevel();
        if (level!=null){
            byId.setLevel(level);
        }
        Long pid = ycUserEditAuth.getPid();
        if (pid!=null){
            byId.setPid(pid);
        }
        Integer state = ycUserEditAuth.getState();
        if (state!=null){
            byId.setState(state);
        }
        boolean realName = ycUserEditAuth.isRealName();
        if (realName == true || realName ==false){
            byId.setRealName(realName);
        }
        Integer type = ycUserEditAuth.getType();
        if (type!=null){
            byId.setType(type);
        }

        ycUserService.update(ycUserMapper.toEntity(byId));
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
